package com.jinyang.jetpackdemo.paging3

import androidx.lifecycle.*
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.jinyang.jetpackdemo.util.LjyLogUtil
import kotlinx.coroutines.flow.Flow

/**
 * @Author:         LiuJinYang
 * @CreateDate:     2021/8/16
 * 3. 生成可观察的数据集
 */
//@ExperimentalPagingApi
class Paging3ViewModel : ViewModel() {
    var num2 = 0
    private val _num = MutableLiveData(0)
    val num3: LiveData<Int> = _num
    fun addNum() {
        _num.value = _num.value?.plus(1)
    }

    fun getPagingData(): Flow<PagingData<Repo>> {
        return Repository.getPagingData().cachedIn(viewModelScope)
    }

    override fun onCleared() {
        super.onCleared()
        LjyLogUtil.d("ViewModel.onCleared")
    }

    fun getPagingData2(): LiveData<PagingData<Repo>> =
        Repository.getPagingData2().cachedIn(viewModelScope).asLiveData()
}